/**
 * 游戏记录
 * @author sherwyn.Tang
 * @create 2015/03/07
 */
;(function (global){
	var CGI = {
			HISTORY		: window.frontPath + 'history/pagingHistory',//查询历史记录
			CANCEL_ORDER: function (){ return window.frontPath + 'lottery/cancelLotteryBets?r=' + new Date().getTime()}, //撤单
			LOTTERY_BET_DETAIL	: function (){ return window.frontPath + 'lottery_bets/detail?r=' + new Date().getTime()}		//是否开奖，未开奖是0 已开奖是开奖数据	
	};
	//初始化输入框默认提示组件
	$.placeholder.init();
	
	 var startDateStr =DateUtil.dateToStr('yyyy-MM-dd');
	 jQuery("#startDate").val(startDateStr+" 00:00:00");
	 
	/**
	 * 按条件查询
	 */
	$('#history-search').click(function (){
		window.P(
			1,
			'#history-search-form' , 
			CGI.HISTORY , 
			'#history-z-template' , 
			'#history-data' , 
			15
		);
	});//.trigger('click');
	
	/**
	 * 撤单
	 */
	$('[data-flag="cancel_order"]').livequery('click' , function (){
		var _this = $(this);

    	$.alert({
    		status : 'help',
			basicTitle : '您确定要撤单？',
			buttons : [{'flag' : 'ok' , 'name' : '确定' , 'handler' : function(){
				$.ajax({ 
				    url: CGI.CANCEL_ORDER(), 
				    dataType: 'json', 
				    data: "lotteryBetsId="+_this.parents('tr').
				    find('[data-flag="bets_order"]').
				    attr('data-id'), 
				    contentType: 'application/json',
				    success: function(data) {
				    	if(data.retcode==0){
				    		$.none_alert(function(){
				    			$.alert({
		    			    		status : 'ok',
		    						basicTitle : '撤单成功！'
		    					});
				    			reloadUserInfo();
				    		});
				    		window.P(
			    				window.Pager.currPage,
			    				'#history-search-form' , 
			    				CGI.HISTORY , 
			    				'#history-z-template' , 
			    				'#history-data' , 
			    				15
			    			);
				    	}else{
				    		$.exception(data.retcode , data.retmsg);
				    	}
				    }
				});
			}},{'flag' : 'cancel' , 'name' : '取消' , 'handler' : function (){
				$.none_alert();
			}}]
		});
	});
	
	//游戏记录详情
	$("#history-table").delegate('#history-data tr', 'click' , function (e){
		var _this = $(this);
		if($(e.target).attr('data-flag') != 'cancel_order'){
			$.ajax({ 
			    url: CGI.LOTTERY_BET_DETAIL(), 
			    dataType: 'json', 
			    data: "id="+$('.bets_order', _this).attr('data-id'), 
			    contentType: 'application/json',
			    success: function(data) {
			    	var ms; 
		    		if(data.bets_mode == 2.00){
		    			ms = '元'
		    		}else if(data.bets_mode == 0.20){
		    			ms = '角'
		    		}else{
		    			ms = '分'
		    		}
			    	$.alert({
						isIcon : false,
						title : '订单详情',
						bigContents : [
						   '投注用户：'+ data.uname,
						   '彩种：'+ data.lottery_name,
						   '玩法：'+ data.play_name,
						   '订单状态：'+ data.betsStatus,
						   '订单编号：'+ data.bets_order,
						   '购买模式：'+ ms ,
						   '奖金返点：'+ data.bets_bonusProp+'－'+data.bets_fanDian+'%' ,
						   '开奖号码：'+ (data.bets_lotteryNo || ''),
						   '投注时间：'+ moment(data.bets_time).format('YYYY-MM-DD HH:mm:ss'),
						   '投注期号：'+ data.lottery_no,
						   '购买倍数：'+ data.bets_beiShu,
						   '开奖时间：'+ (data.bets_kjTime?moment(data.bets_kjTime).format('YYYY-MM-DD HH:mm:ss'):''),
						   '购买金额：'+ (data.bets_amount || '0')+'元',
						   '返点金额：'+ (data.bets_fanDianAmount || '0')+'元',
						   '总注数：'+ (data.bets_num || '0'),
						   '中奖注数：'+ (data.bets_zjCount || '0'),
						   '中奖金额：'+ (data.bets_bonus || '0')+'元',
						   '购买盈亏：'+ (data.bets_lotteryNo ? (data.bets_bonus - data.bets_amount + data.bets_fanDianAmount).toFixed(4) : '0')+'元'
			            ],
						smallTitle : '完整投注内容',
						smallContents : ["<div style='overflow: auto;height:90px;border: 1px solid #ddd;border-bottom: 0px;margin-bottom:-6px'>"+data.bets_data+"</div>"],
						column: 3,
						column_width: 200,
						width: 600
					});
			    }
			});
		}
		return false;
	});

})(window);